<!DOCTYPE html>
<html>
<head>
    <th:block th:include="include :: header('代码生成')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">连接名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="tableName" autocomplete="off" lay-affix="clear" placeholder="连接名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="gen-table" lay-filter="gen-table"></table>
    </div>
</div>

<script type="text/html" id="gen-toolbar">
    <button class="pear-btn pear-btn-md pear-btn-primary" lay-event="import" plain>
        <i class="layui-icon">&#xe654;</i>
        导入
    </button>
    <button class="pear-btn pear-btn-md pear-btn-primary dropdown-export" lay-event="export-down" plain>
        <i class="layui-icon layui-icon-down"></i>
        <span>导出</span>
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchGenCode" plain>
        <i class="layui-icon pear-icon-code"></i>
        生成代码
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchRemove" plain>
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="gen-bar">
    <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">编辑</a>
    <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="preview">预览</a>
    <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="genCode">生成代码</a>
    <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="sync">同步</a>
    <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="remove">删除</a>
</script>


<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd hh:MM:ss')}}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'http','dropdown','tab'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let http = layui.http;
        let tab = layui.tab;
        let dropdown = layui.dropdown;

        let MODULE_PATH = "/table/";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '表名',
                    field: 'tableName',
                    align: 'center'
                },
                {
                    title: '描述',
                    field: 'tableComment',
                    align: 'center'
                },
                {
                    title: '类名',
                    field: 'className',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#gen-bar',
                    align: 'center',
                    width: 320
                }
            ]
        ]
        let tableId = 'gen-table'
        table.render({
            elem: '#gen-table',
            id: tableId,
            url: MODULE_PATH + "page",
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#gen-toolbar',
            text: {none: '暂无信息'},
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(gen-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }
            else if (obj.event === 'edit') {
                window.edit(obj);
            }
            else if(obj.event === 'preview'){
                window.preview(obj);
            }
            else if(obj.event === 'genCode'){
                window.genCode(obj)
            }
        });

        table.on('toolbar(gen-table)', function (obj) {
            if (obj.event === 'import') {
                window.import()
            }
            else if(obj.event === 'export-down'){
                window.dropdown(obj)
            }
            else if(obj.event === 'batchGenCode'){
                window.batchGenCode(obj)
            }
            else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function (data) {
            table.reload(tableId, {
                where: data.field
            })
            return false;
        });

        window.import = function () {
            layer.open({
                type: 2,
                title: '选择数据源',
                shade: 0.1,
                area: ['750px', '90%'],
                content: MODULE_PATH + 'import.html',
                closeBtn:2,
                btn:["确定","关闭"],
                btn1:function (index, layero, that){
                    var body = layer.getChildFrame( 'body' , index);
                    var iframeWin = window[layero.find( 'iframe' )[0][ 'name' ]]; //得到iframe页的窗口对象，执行iframe页的方法：
                    var childObj = iframeWin.getSelected(); //调用子页面的方法，得到子页面返回的ids
                    console.log(childObj)
                    http.request({
                        url: "/table/import/"+childObj.datasourceId,
                        contentType: http.contentType.json,
                        data:childObj.tabs,
                        method:"post"
                    }).then(res=>{
                        window.refresh()
                        layer.close(index);
                    })
                    return false
                },
                btn2:function (index, layero, that){
                    layer.close(index);
                    return false
                }
            })
        }

        window.dropdown = function (obj){
            dropdown.render({
                // 绑定元素选择器，此处指向 class 可同时绑定多个元素
                elem: '.dropdown-export',
                show: true,
                data: [{title: 'html',id: 100},{title: 'pdf',id: 101},{title: 'md',id: 102}],
                click: function(obj){
                    let type = obj.title
                    let checks = table.checkStatus(tableId);
                    if(checks.data.length === 0){
                        layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    let tableIds = common.getKeyArr(checks.data,"id")

                    let param = {type:type,tableIds:tableIds}
                    http.downFile("/generate/genFile",param).then(res=>{
                        common.downloadFile(res,'tables',type)
                    })
                    // this.elem.find('span').text(obj.title);
                }
            });
        }

        window.edit = function (obj) {
            let title = obj.data['tableName']
            let id = common.hashCode(title)
            top.layui.admin.addTab(id,`编辑配置·${title}`, MODULE_PATH + 'table-edit?id=' + obj.data["id"])
            // layer.open({
            //     type: 2,
            //     title: '编辑配置',
            //     shade: 0.1,
            //     area: ['80%', '90%'],
            //     content: MODULE_PATH + 'table-edit?id=' + obj.data["id"],
            // });
        }

        window.preview = function (obj){
            const size = common.getWindowSize();
            console.log(size)
            layer.open({
                type: 2,
                title: '预览代码',
                shade: 0.1,
                area: [size.width, size.height],
                maxmin:true,
                content: '/generate/preview?tableId=' + obj.data["id"],
            });
        }

        window.genCode = function (obj){
            layer.confirm('确定要生成代码吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                let tableIds = [obj.data['id']]
                http.post("/generate/code",{tableIds:tableIds}).then(res=>{
                    console.log(res)
                    if(res){
                        layer.close(index);
                        layer.msg(res.message, {icon: 1,time: 1000})
                    }
                })
            });
        }

        window.batchGenCode = function (obj) {
            let checks = table.checkStatus(tableId);
            if(checks.data.length === 0){
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let tableIds = common.getKeyArr(checks.data,"id")
            http.post("/generate/code",{tableIds:tableIds}).then(res=>{
                console.log(res)
                if(res){
                    layer.close(index);
                    layer.msg(res.message, {icon: 1,time: 1000})
                }
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该信息', {
                icon: 3,
                title: '提示'
            }, function (index) {
                let arr = [obj.data['id']]
                http.request({
                    url: MODULE_PATH + "delete",
                    contentType: http.contentType.json,
                    data:arr,
                    method:"delete"
                }).then(res=>{
                    layer.close(index);
                    layer.msg(res.message, {icon: 1,time: 1000}, function () {
                        table.reload(tableId);
                    });
                })
            });
        }

        window.batchRemove = function (obj) {
            let checkIds = table.checkStatus(obj.config.id).data;
            if (checkIds.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = [];
            for (let i = 0; i < checkIds.length; i++) {
                ids.push(checkIds[i].id)
            }
            layer.confirm('确定要删除这些数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                http.request({
                    url: MODULE_PATH + "delete",
                    contentType: http.contentType.json,
                    data:ids,
                    method:"delete"
                }).then(res=>{
                    layer.close(index);
                    layer.msg(res.message, {icon: 1,time: 1000}, function () {
                        table.reload(tableId);
                    });
                })
            });
        }

        window.setTables = function (arr){
            console.log(arr)
        }

        window.refresh = function (param) {
            table.reload(tableId);
        }
    })
</script>
</body>
</html>
